package com.nageoffer.jobtrain.projects.elasticsearch;

import com.nageoffer.jobtrain.projects.dao.entity.OjQuestionESDO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface OjQuestionRepository extends ElasticsearchRepository<OjQuestionESDO, Long> {
    /**
     * 根据题目难度进行分页匹配
     *
     * @param difficulty 题目难度
     * @param pageable   分页参数
     * @return OJ题目列表
     */
    Page<OjQuestionESDO> findOjQuestionByDifficulty(Integer difficulty, Pageable pageable);

    /**
     * 根据题目标题、正文关键词进行匹配
     *
     * @param keywordTitle   题目关键词
     * @param keywordContent 正文关键词
     * @param pageable       分页参数
     * @return OJ题目列表
     */
    @Query("{\"bool\": {\"should\": [{ \"match\": { \"title\": \"?0\" } }, { \"match\": { \"content\": \"?1\" } }], \"minimum_should_match\": 1}}")
    Page<OjQuestionESDO> findByTitleOrContent(String keywordTitle, String keywordContent, Pageable pageable);

    /**
     * 根据题目标题、正文关键词、难度一起进行匹配
     *
     * @param keywordTitle   题目关键词
     * @param keywordContent 正文关键词
     * @param difficulty     题目难度
     * @param pageable       分页参数
     * @return OJ题目列表
     */
    @Query("{\"bool\": {\"should\": [{ \"match\": { \"title\": \"?0\" } }, { \"match\": { \"content\": \"?1\" } }], \"minimum_should_match\": 1, \"must\": [{\"term\": {\"difficulty\": \"?2\"}}]}}")
    Page<OjQuestionESDO> findByTitleOrContentAndDifficulty(String keywordTitle, String keywordContent, Integer difficulty, Pageable pageable);
}